package com.szholly.pro.watergas_server.common.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class WebContext {
private static ThreadLocal<HttpServletRequest> ThreadRequest =new ThreadLocal<HttpServletRequest>();
	
	private static ThreadLocal<HttpServletResponse> ThreadResponse =new ThreadLocal<HttpServletResponse>();
	
	
	public static void setRequest(HttpServletRequest httpServletRequest)
	{
		ThreadRequest.set(httpServletRequest);
		DiskFileItemFactory f = new DiskFileItemFactory(10485760,new File("D:/tem/"));
		ServletFileUpload upload=new ServletFileUpload(f); 
		upload.setHeaderEncoding("utf-8");  
	}
	
	public static HttpServletRequest getRequest()
	{
		ServletRequestAttributes requestAttr = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttr.getRequest();
		return request;
	}
	
	public static void setResponse(HttpServletResponse httpServletResponse)
	{
		httpServletResponse.setContentType("text/html;charset=UTF-8");
		ThreadResponse.set(httpServletResponse);
	}
	
	public static HttpServletResponse getResponse()
	{
		HttpServletResponse response = ThreadResponse.get();
		return response;
	}
	
	public static String getWebInitParameter(String key){
		String value = getRequest().getSession().getServletContext().getInitParameter(key);
		return value;
	}
	
	public static String getSessionId(){
		String sessionId = getRequest().getSession().getId();
		return sessionId;
	}
	
	/**
	 * 获取webapp服务器物理路径
	 * @return
	 */
	public static String getServerPath(){
		String path = getRequest().getSession().getServletContext().getRealPath("");
		return path;
	}
}
